﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace BackgroundTaskLib.TestUI
{
    public class BackgroundTaskViewModel : BaseViewModel
    {
        public BackgroundTaskViewModel(BackgroundTask task)
        {
            Task = task;

            Refresh();
        }

        public BackgroundTask Task { get; private set; }

        #region Dependency Properties

        #region Name

        public string Name
        {
            get { return (string)GetValue(NameProperty); }
            set { SetValue(NameProperty, value); }
        }

        public static readonly DependencyProperty NameProperty =
            DependencyProperty.Register("Name", typeof(string), typeof(BackgroundTaskViewModel), new PropertyMetadata(null));

        #endregion

        #region Status

        public TaskStatus Status
        {
            get { return (TaskStatus)GetValue(StatusProperty); }
            set { SetValue(StatusProperty, value); }
        }

        public static readonly DependencyProperty StatusProperty =
            DependencyProperty.Register("Status", typeof(TaskStatus), typeof(BackgroundTaskViewModel), new PropertyMetadata(TaskStatus.Created));

        #endregion

        #region Progress

        public double Progress
        {
            get { return (double)GetValue(ProgressProperty); }
            set { SetValue(ProgressProperty, value); }
        }

        public static readonly DependencyProperty ProgressProperty =
            DependencyProperty.Register("Progress", typeof(double), typeof(BackgroundTaskViewModel), new PropertyMetadata(0.0));

        #endregion

        #region IsIndeterminate

        public bool IsIndeterminate
        {
            get { return (bool)GetValue(IsIndeterminateProperty); }
            set { SetValue(IsIndeterminateProperty, value); }
        }

        public static readonly DependencyProperty IsIndeterminateProperty =
            DependencyProperty.Register("IsIndeterminate", typeof(bool), typeof(BackgroundTaskViewModel), new PropertyMetadata(false));

        #endregion

        #region HasException

        public bool HasException
        {
            get { return (bool)GetValue(HasExceptionProperty); }
            set { SetValue(HasExceptionProperty, value); }
        }

        public static readonly DependencyProperty HasExceptionProperty =
            DependencyProperty.Register("HasException", typeof(bool), typeof(BackgroundTaskViewModel), new PropertyMetadata(false));

        #endregion

        #region IsCancelled

        public bool IsCancelled
        {
            get { return (bool)GetValue(IsCancelledProperty); }
            set { SetValue(IsCancelledProperty, value); }
        }

        public static readonly DependencyProperty IsCancelledProperty =
            DependencyProperty.Register("IsCancelled", typeof(bool), typeof(BackgroundTaskViewModel), new PropertyMetadata(false));

        #endregion

        #region IsRemoving

        public bool IsRemoving
        {
            get { return (bool)GetValue(IsRemovingProperty); }
            set { SetValue(IsRemovingProperty, value); }
        }

        public static readonly DependencyProperty IsRemovingProperty =
            DependencyProperty.Register("IsRemoving", typeof(bool), typeof(BackgroundTaskViewModel), new PropertyMetadata(false));

        #endregion

        #endregion

        public void Refresh()
        {
            Name = Task.Name;
            Status = Task.Status;
            Progress = Task.Progress;
            IsIndeterminate = Task.IsIndeterminate;

            HasException = Task.Status == TaskStatus.Exception;
            IsCancelled = Task.Status == TaskStatus.Cancelling || Task.Status == TaskStatus.Cancelled;
        }

        public void OnRemoving()
        {
            IsRemoving = true;
        }

        public static bool CheckIsHidden(BackgroundTask task)
        {
            return
                task.Tag == BackgroundTask.SystemTaskTag &&
                task.Status != TaskStatus.Exception &&
                task.Status != TaskStatus.Cancelling &&
                task.Status != TaskStatus.Cancelled;
        }
    }
}
